//
//  UWApi.m
//  Urban Words
//
//  Created by Tim Brandin on 2011-11-25.
//  Copyright (c) 2011 Sypreme. All rights reserved.
//

#import "UWApi.h"

#import "UWAppDelegate.h"

@implementation UWApi

static NSString *server = @"http://sypreme.se/uw/api";

// CREATE



// READ

// Deprecated:
+ (void)call:(NSString *)method delegate:(id)delegate 
{
    [UWApi read:method withParams:nil delegate:delegate];
}

// Deprecated
+ (void)call:(NSString *)method withParams:(NSDictionary *)params delegate:(id)delegate;
{
    [UWApi read:method withParams:params delegate:delegate];
}

+ (void)read:(NSString *)method delegate:(id)delegate
{
    [UWApi read:method withParams:nil delegate:delegate];
}
// Deprecated
+ (void)read:(NSString *)method withParams:(NSDictionary *)params delegate:(id)delegate
{
    UWRequest *request = [[UWRequest alloc] initWithMethod:method atServer:server operation:READ];
    
    request.delegate = delegate;
    
    [request start];
}

+ (void)read:(NSString *)method listener:(id)object action:(SEL)selector
{
    [UWApi read:method withParams:nil listener:object action:selector];
}

+ (void)read:(NSString *)method withParams:(NSDictionary *)params listener:(id)object action:(SEL)selector
{
    UWRequest *request = [[UWRequest alloc] initWithMethod:method atServer:server operation:READ];
    
    [request addListener:object action:selector];
    
    [request start];
}

// Deprecated
+ (void)define:(NSString *)word delegate:(id)delegate 
{
    NSString *ud_server = @"http://www.urbandictionary.com/iphone/search/define";
    
    NSString *method = [NSString stringWithFormat:@"?term=%@", word];
    
    UWRequest *request = [[UWRequest alloc] initWithMethod:method atServer:ud_server operation:READ];
    
    request.delegate = delegate;
    
    [request start];
}

+ (void)define:(NSString *)word listener:(id)object action:(SEL)selector
{
    NSString *ud_server = @"http://www.urbandictionary.com/iphone/search/define";
    
    NSString *method = [NSString stringWithFormat:@"?term=%@", word];
    
    UWRequest *request = [[UWRequest alloc] initWithMethod:method atServer:ud_server operation:READ];
    
    [request addListener:object action:selector];
    
    [request start];
}



// UPDATE
// Deprecated
+ (void)update:(NSString *)method withData:(NSDictionary *)data delegate:(id)delegate
{
    [UWApi update:method withData:data withParams:nil delegate:delegate];
}

// Deprecated
+ (void)update:(NSString *)method withData:(NSDictionary *)data withParams:(NSDictionary *)params delegate:(id)delegate
{
    UWRequest *request = [[UWRequest alloc] initWithMethod:method atServer:server operation:UPDATE];
    
    request.delegate = delegate;
    
    [request setPayload:data];
    
    [request start];
}

+ (void)update:(NSString *)method withData:(NSDictionary *)data 
      listener:(id)object action:(SEL)selector
{
    [UWApi update:method withData:data withParams:nil listener:object action:selector];
}

+ (void)update:(NSString *)method withData:(NSDictionary *)data 
    withParams:(NSDictionary *)params  listener:(id)object action:(SEL)selector
{
    UWRequest *request = [[UWRequest alloc] initWithMethod:method atServer:server operation:UPDATE];
    
    [request addListener:object action:selector];
    
    [request setPayload:data];
    
    [request start];
}

// DELETE



/**
 * parse json data
 */
+ (id)parseJson:(NSData *)data {
    
    NSString* responseString = [[[NSString alloc] initWithData:data
                                                      encoding:NSUTF8StringEncoding]
                                autorelease];
    
    SBJSON *jsonParser = [[SBJSON new] autorelease];
    
    id result = [jsonParser objectWithString:responseString];
    
    return result;
    
}

@end
